/*********************************************************************************
 *      Copyright:  (C) 2024 dengleen<dengleen@outlook.com>
 *                  All rights reserved.
 *
 *       Filename:  main.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(06/05/24)
 *         Author:  dengleen<dengleen@outlook.com>
 *      ChangeLog:  1, Release initial version on "06/05/24 10:29:20"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>

#include "iniparser.h"
#include "mqtt.h"
#include "conf.h"

int get_mqtt_parameter1(char *ini_path, mqtt_ctx_t *mqtt_ctx, int option)
{
	dictionary                *ini=NULL;
	ini_ctx_t                 ini_ctx;


	ini = iniparser_load(ini_path);
	if(ini == NULL)
	{
		return -1;
	}

	if(option == 1)
	{
	ini_ctx.hostname = iniparser_getstring(ini, "huaweiyun:hostname", NULL);
	ini_ctx.clientid = iniparser_getstring(ini, "huaweiyun:clientid", NULL);
	ini_ctx.username = iniparser_getstring(ini, "huaweiyun:username", NULL);
	ini_ctx.password = iniparser_getstring(ini, "huaweiyun:password", NULL);
	ini_ctx.port = iniparser_getint(ini, "huaweiyun:port", 1883);

	ini_ctx.pub_topic = iniparser_getstring(ini, "huaweiyun:pub_topic", NULL);
	ini_ctx.sub_topic = iniparser_getstring(ini, "huaweiyun:sub_topic", NULL);
	//ini_ctx.payload = iniparser_getstring(ini, "huaweiyun:payload", NULL);
	ini_ctx.keepalive = iniparser_getint(ini, "huaweiyun:keepalive", 60);
	ini_ctx.qos = iniparser_getint(ini, "huaweiyun:qos", 0);

#if 0
	printf("ini_ctx.hostname = %s\n", ini_ctx.hostname);
	printf("ini_ctx.clientid = %s\n", ini_ctx.clientid);
	printf("ini_ctx.username = %s\n", ini_ctx.username);
	printf("ini_ctx.password = %s\n", ini_ctx.password);
	printf("ini_ctx.port = %d\n", ini_ctx.port);

	printf("ini_ctx.pub_topic = %s\n", ini_ctx.pub_topic);
	printf("ini_ctx.sub_topic = %s\n", ini_ctx.sub_topic);
	printf("ini_ctx.payload = %s\n", ini_ctx.payload);
	printf("ini_ctx.keepalive = %d\n", ini_ctx.keepalive);
	printf("ini_ctx.qos = %d\n", ini_ctx.qos);

#endif	

	}

	if(option == 2)
	{
	
	ini_ctx.hostname = iniparser_getstring(ini, "aliyun:hostname", NULL);
	ini_ctx.clientid = iniparser_getstring(ini, "aliyun:clientid", NULL);
	ini_ctx.username = iniparser_getstring(ini, "aliyun:username", NULL);
	ini_ctx.password = iniparser_getstring(ini, "aliyun:password", NULL);
	ini_ctx.port = iniparser_getint(ini, "aliyun:port", 1883);

	ini_ctx.pub_topic = iniparser_getstring(ini, "aliyun:pub_topic", NULL);
	ini_ctx.sub_topic = iniparser_getstring(ini, "aliyun:sub_topic", NULL);
	//ini_ctx.payload = iniparser_getstring(ini, "aliyun:payload", NULL);
	ini_ctx.keepalive = iniparser_getint(ini, "aliyun:keepalive", 60);
	ini_ctx.qos = iniparser_getint(ini, "aliyun:qos", 0);
	}

	snprintf(mqtt_ctx->hostname, MQTT_SIZE1, "%s", ini_ctx.hostname);
	snprintf(mqtt_ctx->clientid, MQTT_SIZE1, "%s", ini_ctx.clientid);
	snprintf(mqtt_ctx->username, MQTT_SIZE1, "%s", ini_ctx.username);
	snprintf(mqtt_ctx->password, MQTT_SIZE1, "%s", ini_ctx.password);
	mqtt_ctx->port = ini_ctx.port;

	snprintf(mqtt_ctx->pub_topic, MQTT_SIZE1, "%s", ini_ctx.pub_topic);
	snprintf(mqtt_ctx->sub_topic, MQTT_SIZE1, "%s", ini_ctx.sub_topic);
	//snprintf(mqtt_ctx->payload, MQTT_SIZE1, "%s", ini_ctx.payload);
	mqtt_ctx->keepalive = ini_ctx.keepalive;
	mqtt_ctx->qos = ini_ctx.qos;

	iniparser_freedict(ini);
	return 0;


}
